<?php
/**
 * 文章分类控制器
 * Created by PhpStorm.
 * User: geyouwen
 * Date: 16/7/15
 * Time: 下午5:22
 */

namespace Backend\Controller;


use Org\Util\Tree;
use Think\Exception;

class ArticleCateController extends BaseController
{
    private $mod;
    public function __construct()
    {
        parent::__construct();
        $this->mod = D('article_cate');
    }

    public function index()
    {
        $tree = new Tree();
        $tree->icon = array('│ ','├─ ','└─ ');
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        $result = $this->mod->order('ordid asc')->select();
        $array = array();
        $cate_conf = array(
            '1' => '文章栏目',
            '2' => '单页栏目',
        );
        foreach($result as $r) {
            $r['status'] = ($r['status'] ==1) ? '<i class="layui-icon layui-icon-ok">&#xe605;</i>' : '<i class="layui-icon layui-icon-no">&#x1006;</i>';
            $r['str_status'] = '<img data-tdtype="toggle" data-id="'.$r['id'].'" data-field="status" data-value="'.$r['status'].'" src="__STATIC__/images/admin/toggle_' . ($r['status'] == 0 ? 'disabled' : 'enabled') . '.gif" />';
            $r['str_manage'] = '<a href="'.U('backend/article_cate/add',array('pid'=>$r['id'])).'">'.L('add_article_subcate').'</a> |
                                <a href="'.U('backend/article_cate/edit',array('id'=>$r['id'])).'"><i class="layui-icon layui-icon-edit">&#xe642;</i>'.L('edit').'</a> |
                                <a href="javascript:;" class="layer_confirm" data-url="'.U('backend/article_cate/delete',array('id'=>$r['id'])).'" data-msg="'.sprintf(L('confirm_delete_one'),$r['cate_name']).'"><i class="layui-icon layui-icon-del">&#xe640;</i>'.L('delete').'</a>';
            $r['parentid_node'] = ($r['pid'])? ' class="child-of-node-'.$r['pid'].'"' : '';
            $r['cate_type'] = $cate_conf[$r['type']];
            $array[] = $r;
        }
        $str  = "<tr id='node-\$id' \$parentid_node>
                <td align='left'><input type='checkbox' value='\$id' class='J_checkitem'></td>
                <td><div class='th-inner al-l'>\$spacer<span data-tdtype='edit' data-field='cate_name' data-id='\$id' class='tdedit'>\$cate_name</span></div></td>
                <td align='left'>\$id</td>
                <td align='left'>\$cate_type</td>
                <td align='left'><span data-tdtype='edit' data-field='ordid' data-id='\$id' class='tdedit'>\$ordid</span></td>
                <td align='left'>\$status</td>
                <td align='left' width='200'>\$str_manage</td>
                </tr>";
        $tree->init($array);
        $list = $tree->get_tree(0, $str);
        $this->assign('list', $list);
        $this->assign('list_table', true);
        $this->display();
    }


    public function add()
    {
        if(IS_AJAX)
        {
            $this->mod->create();
            $cate_id = $this->mod->add();
            //cate_path修改 start
            $cate_path = $this->mod->where(array('id' => I('pid')))->getField('cate_path');
            $cate_path = $cate_path ? $cate_path . '|' . $cate_id : $cate_id;
            $this->mod->where(array('id' => $cate_id))->save(array('cate_path' => $cate_path));
            //cate_path修改 end
            $return = array(
                'status' => 1,
                'msg'    => L('operation_success'),
            );
            S('cache_cate_list',null);
            $this->ajaxReturn($return);
        }
        $tree = new Tree();
        $result = $this->mod->select();
        $array = array();
        foreach($result as $r) {
            $r['selected'] = $r['id'] == $_GET['pid'] ? 'selected' : '';
            $array[] = $r;
        }
        $str  = "<option value='\$id' \$selected>\$spacer \$cate_name</option>";
        $tree->init($array);
        $select_menus = $tree->get_tree(0, $str);
        $this->assign('select_menus', $select_menus);
        $this->display();
    }


    public function edit()
    {
        $id = I('id');
        if(IS_AJAX)
        {
            $data = array(
                'pid'        => I('pid',0),
                'cate_name'  => I('cate_name'),
                'type'       => I('type',1),
                'status'     => I('status',1),
                'seo_title'  => I('seo_title'),
                'seo_keywords'    => I('seo_keywords'),
                'seo_description' => I('seo_description'),
            );
            if(!$data['cate_name'])
            {
                $return = array(
                    'status' => 0,
                    'msg'    => '请输入分类名称',
                );
                $this->ajaxReturn($return);
            }
            try{
                //cate_path修改 start
                $cate_path = $this->mod->where(array('id' => I('pid')))->getField('cate_path');
                $data['cate_path'] = $cate_path ? $cate_path . '|' . $id : $id;
                //cate_path修改 end
                $this->mod->where(array('id'=>$id))->save($data);
                S('cache_cate_list',null);
                $return = array(
                    'status' => 1,
                    'msg'    => L('operation_success'),
                );
                $this->ajaxReturn($return);
            } catch(Exception $e)
            {
                $msg = $e->getMessage();
                $return = array(
                    'status' => 0,
                    'msg'    => $msg,
                );
                $this->ajaxReturn($return);
            }
        }
        $cate = $this->mod->find($id);
        if(!$cate)
        {
            $this->error('数据不存在', u('backend/article_cate/index'));
            exit;
        }
        $tree = new Tree();
        $result = $this->mod->select();
        $array = array();
        foreach($result as $r) {
            $r['selected'] = $r['id'] == $cate['pid'] ? 'selected' : '';
            $array[] = $r;
        }
        $str  = "<option value='\$id' \$selected>\$spacer \$cate_name</option>";
        $tree->init($array);
        $select_menus = $tree->get_tree(0, $str);
        $this->assign('select_menus', $select_menus);
        $this->assign('cate', $cate);
        $this->display();
    }

    public function delete()
    {
        if(IS_AJAX)
        {
            $id = I('id');
            $where = array('id' => $id);
            if(isset($_POST['params']))
            {
                $params = $_POST['params'];
                $where = array(
                    'id' => array('in', $params)
                );
            }
            try{
                $this->mod->where($where)->delete();
                $return = array(
                    'status' => 1,
                    'msg'    => L('operation_success'),
                );
                $this->ajaxReturn($return);

            } catch (Exception $e)
            {
                $msg = $e->getMessage();
                $return = array(
                    'status' => 0,
                    'msg'    => $msg,
                );
                $this->ajaxReturn($return);
            }
        }
    }


    //ajax方式获取分类信息
    public function ajax_getchilds()
    {
        $pid = intval(I('pid'));
        $this->ajaxReturn($this->mod-> ajaxchildren($pid));
    }

}